#include "TemplateHooks.h"
#include "ObjectFactory.h"
#include "I18n.h"

namespace phnq
{
	namespace phnqlets
	{
		static const void* CREATE_OBJECT_FN_USER_DATA = "phnq.phnqlets.TemplateHooks.createObjectFnUserData";
		static const void* ENCODE_URL_FN_USER_DATA = "phnq.phnqlets.TemplateHooks.encodeUrlFnUserData";
		static const void* INSERT_PHNQLET_FN_USER_DATA = "phnq.phnqlets.TemplateHooks.insertPhnqletFnUserData";
		static const void* PRINT_FN_USER_DATA = "phnq.phnqlets.TemplateHooks.printFnUserData";
		static const void* ADD_DEP_FN_USER_DATA = "phnq.phnqlets.TemplateHooks.addDependencyFnUserData";
		
		TemplateHooks::TemplateHooks()
		{
			globals = new UTHObject();
			
			UTHObject *phnqObj = new UTHObject();
			UTHFunction *newObjFn = new UTHFunction(this, CREATE_OBJECT_FN_USER_DATA);
			UTHFunction *encUrlFn = new UTHFunction(this, ENCODE_URL_FN_USER_DATA);
			UTHFunction *insertPhnqletFn = new UTHFunction(this, INSERT_PHNQLET_FN_USER_DATA);
			UTHFunction *printFn = new UTHFunction(this, PRINT_FN_USER_DATA);
			UTHFunction *addDependencyFn = new UTHFunction(this, ADD_DEP_FN_USER_DATA);
			phnqObj->set("createObject", newObjFn);
			phnqObj->set("encodeUrl", encUrlFn);
			phnqObj->set("insertPhnqlet", insertPhnqletFn);
			phnqObj->set("print", printFn);
			phnqObj->set("addDependency", addDependencyFn);
			globals->set("phnq", phnqObj);
			
			phnqObj->release();
			newObjFn->release();
			encUrlFn->release();
			insertPhnqletFn->release();
			printFn->release();
			addDependencyFn->release();
		}

		TemplateHooks::~TemplateHooks()
		{
			globals->release();
		}
		
		UTHObject* TemplateHooks::getGlobalObject()
		{
			return globals;
		}
		
		UTHValue* TemplateHooks::invokeFunction(UTHFunction *fn, UTHArray* args)
		{
			const void *userData = fn->getUserData();
			
			if(userData == CREATE_OBJECT_FN_USER_DATA)
			{
				ObjectFactory *factory = ObjectFactory::getCurrent();

				if(factory)
					return factory->createObject(args);
				else
					return UTHNull::getInstance();
			}
			else if(userData == ENCODE_URL_FN_USER_DATA)
			{
				UTHValue *firstArg = args->get(0);
				if(firstArg->isString())
				{
					UTHString *urlStr = (UTHString*)firstArg;
					string s(urlStr->getChars());
					if(s.find_first_of("?") == string::npos)
					{
						s += "?l=" + I18n::getCurrentLocale();
					}
					else
					{
						s += "&l=" + I18n::getCurrentLocale();
					}
					return new UTHString(s.c_str());
				}
			}
			else if(userData == INSERT_PHNQLET_FN_USER_DATA)
			{
				
			}
			else if(userData == PRINT_FN_USER_DATA)
			{
				for(int i=0; i<args->length(); i++)
				{
					if(i > 0)
						cout << " ";

					UTHValue *arg = args->get(i);
					if(arg->isString())
						cout << ((UTHString*)arg)->getChars();
					else
						cout << arg->toString();

					cout << endl;
				}
				return UTHUndefined::getInstance();
			}
			else if(userData == ADD_DEP_FN_USER_DATA)
			{
				if(args->length() > 0)
				{
					UTHValue *firstArg = args->get(0);
					if(firstArg->isString())
					{
						UTHString *typeStr = (UTHString*)firstArg;
						
						
						//cout << "add dep: " << typeStr->getChars() << endl;
					}
				}
				
			}
			

			return UTHNull::getInstance();
		}
	}
}
